<template>
  <view class="box">
    <view class="top tou fixed" :style="'padding-top:' + statusBarHeight1 + 'px'">
      <view class="title">
        <image
          v-if="pages.length > 1"
          class="back"
          src="https://pic.bangbangtongcheng.com/static/back.png"
          @click="back"
        />
        <image
          v-else
          class="back"
          src="https://pic.bangbangtongcheng.com/fc915aba003f4504a80d16c7a8c9b348"
          @click="back"
        />
        推荐有礼
      </view>
    </view>
    <view class="top tou" :style="'padding-top:' + statusBarHeight1 + 'px'"></view>
    <image
      class="png"
      src="https://pic.bangbangtongcheng.com/d6b58dd649e4458497164e230505924e.png"
      mode="widthFix"
    />
  </view>
</template>

<script>
import { mapGetters } from "vuex";
let statusBarHeight;
//#ifdef H5
import wxshare from "../utils/index.js";
statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 10;
//#endif
// #ifdef APP-PLUS
statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 10;
//  #endif
// #ifdef MP-WEIXIN
statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 5;
//  #endif
export default {
  data() {
    return {
      statusBarHeight,
      userId: "",
      taskId: "",
      registCity: "",
      latitude: "",
      longitude: "",
      userOpenid: "",
      pages: [],
    };
  },
  computed: {
    ...mapGetters(["statusBarHeight1", "userinfo"]),
  },
  async onLoad(options) {
    let that = this;
    this.pages = getCurrentPages();
    /* #ifdef H5 */
    this.jinzhi();
    /* #endif */
    if (options.userId) {
      this.userId = options.userId;
      this.taskId = options.taskId;
    }
    if (uni.getStorageSync('locationState') == 1) {
      await this.getLocation();
    }
  },
  methods: {
    async getLocation() {
      let that = this;
      // #ifdef APP-PLUS
      let isAndroid = false;
      const res = uni.getSystemInfoSync();
      if (res.platform == "android") {
        isAndroid = true;
      } else {
        isAndroid = false;
      }
      if (isAndroid) {
        that.authorizationApp(
          "locationState",
          "android.permission.ACCESS_FINE_LOCATION",
          "您已关闭位置权限或获取位置信息失败",
          that.getLocation1()
        );
      } else {
        that.getLocation1();
      }
      //  #endif
      // #ifndef APP-PLUS
      that.getLocation1();
      //  #endif
    },
    async getAddress() {
      uni.showLoading({
        title: "加载中",
        mask: true,
      });
      let lat = this.latitude;
      let lng = this.longitude;
      let that = this;
      await uni.request({
        url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=ZSTBZ-AQQKW-5SURG-YN5ZG-VYJO7-2YFQR`, //仅为示例，并非真实接口地址。
        success: (res) => {
          that.registCity = res.data.result.address_component.district;
          setTimeout(() => {
            uni.hideLoading();
            // #ifdef MP-WEIXIN
            let wxUrl =
              `https://www.bangbangtongcheng.com/conven/share/follow?userId=${this.userId}&taskNo=${this.taskId}&registCity=${this.registCity}`;
            // let wxUrl = `https://ws.weisou.online/conven/share/follow?userId=${this.userId}&taskNo=${this.taskId}&registCity=${this.registCity}`;
            console.log(wxUrl);
            uni.navigateTo({
              url: "/pages_my/webview?wxUrl=" + encodeURIComponent(wxUrl),
            });
            // #endif
          }, 1500);
        },
      });
    },
    async getLocation1() {
      let _this = this;
      uni.showLoading({
        title: "加载中",
        mask: false,
      });
      await uni.getLocation({
        type: "gcj02",
        // highAccuracyExpireTime: "3000",
        async success(res) {
          _this.latitude = res.latitude;
          _this.longitude = res.longitude;
          await _this.getAddress();
          // _this.getFy()
        },
        fail(res) {
          console.log(res, "经纬度没有授权");
          uni.setStorageSync("locationState", 0);
          if (uni.getStorageSync("locationState") == 0) {
            uni.showModal({
              title: "提示",
              content: "您已关闭位置权限或获取位置信息失败",
              success: function (res) {
                if (res.confirm) {
                  // plus.runtime.openURL("app-settings:");
				  if (plus.os.name === 'iOS') {
				  	plus.runtime.openURL("app-settings://");
				  } else {
				  	var Intent = plus.android.importClass("android.content.Intent");
				  	var Settings = plus.android.importClass("android.provider.Settings");
				  	var Uri = plus.android.importClass("android.net.Uri");
				  	var mainActivity = plus.android.runtimeMainActivity();
				  	var intent = new Intent();
				  	intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
				  	var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
				  	intent.setData(uri);
				  	mainActivity.startActivity(intent);
				  }
                } else if (res.cancel) {
                  console.log("用户点击取消");
                }
                // _this.getFy()
              },
            });
            return false;
          }
        },
        complete() {
          uni.hideLoading();
        },
      });
    },
    jinzhi() {
      let _this = this;
      let param = window.location.href.split("#")[0];
      uni.request({
        method: "post",
        data: {
          url: param,
        },
        header: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: "https://www.bangbangtongcheng.com/conven//mob/login/getJsTicket.do",
        success(re) {
          _this.jinzhifx(re.data);
        },
      });
    },
    jinzhifx(data) {
      wxshare.config({
        debug: false, //是否打开调试
        appId: data.appId, // 公众号的唯一标识
        timestamp: data.timestamp, // 生成签名的时间戳
        nonceStr: data.nonce, // ，生成签名的随机串
        signature: data.signature, // 签名
        jsApiList: ["hideMenuItems"],
      });
      // 禁止分享
      wxshare.hideMenuItems({
        // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
        menuList: [
          "menuItem:share:appMessage",
          "menuItem:share:timeline",
          "menuItem:share:timeline",
          "menuItem:share:qq",
          "menuItem:favorite",
          "menuItem:share:QZone",
          "menuItem:openWithSafari",
        ],
      });
    },

    back() {
      let pages = getCurrentPages();
      if (pages.length > 1) {
        uni.navigateBack({
          delta: 1,
        });
      } else {
        uni.switchTab({ url: "/pages/index/index" });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  background: #fef3e2;
  min-height: 100vh;
}
.png {
  width: 100%;
}
.top {
  background: #fff;
  .title {
    // text-align: center;
    height: 88rpx;
    line-height: 88rpx;
    font-size: 36rpx;
    padding: 0 37rpx;
    text-align: center;
    font-size: 34rpx;
    color: #000;
  }

  .back {
    width: 40rpx;
    height: 40rpx;
  }
  .image1 {
    position: absolute;
    width: 32rpx;
    height: 32rpx;
    // top: 116rpx;
    bottom: 28rpx;
    left: 30rpx;
  }
}
.fixed {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
}
.view {
  height: 100px;
}
</style>
